package controllers.common.interceptor;


import models.common.bean.CurrUser;

import org.apache.commons.lang.StringUtils;

import play.mvc.Before;
import play.mvc.Controller;

import common.annotation.EnterpriseCertification;
import common.annotation.PaymentAccountCheck;
import common.annotation.RealNameCheck;
import common.constants.ConfConst;
import common.constants.Constants;
import common.utils.ResultInfo;

import controllers.common.FrontBaseController;

/**
 * 前台-账户中心拦截器
 *
 * @description 
 *
 * @author huangyunsong
 * @createDate 2015年12月17日
 */
public class AccountInterceptor extends Controller{
	
	/**
	 * 登陆拦截
	 */
	@Before(priority=11)
	static void checkLogin(){
		CurrUser user = FrontBaseController.getCurrUser();
		if (user != null) {
			
			return;
		}

		boolean isAjax = request.isAjax();
		if(isAjax){
			
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_LOGIN;
			result.msg = "没有登录，或者登录状态已经失效!请重新登录!";
			
			renderJSON(result);
		} else{
			//redirect("front.LoginAndRegisteCtrl.loginPre");
			redirect("front.FrontHomeCtrl.frontHomePre");
		}
	}
	
	/**
	 * 资金托管账户开户拦截
	 */
	/*@Before(priority=12)*/
	static void checkPaymentAccount(){
		PaymentAccountCheck checkAction = getActionAnnotation(PaymentAccountCheck.class);
		
			return;
	}
	
	/**
	 * 用户未实名认证拦截器
	 */
	@Before(priority=13)
	static void checkRealName(){
		RealNameCheck realNameCheck = getActionAnnotation(RealNameCheck.class);
		if (realNameCheck == null) {
			
			return;
		}
		
		/* 获取登录当前对象 */
		CurrUser user = FrontBaseController.getCurrUser();
		if (user == null) {
			
			checkLogin();
		}
		//企业不需要实名认证
		if (user.is_company == 1) {
			return;
		}
		if (user.is_real_name) {
			
			return;
		}
		//企业不需要实名认证
		if (user.is_company == 1) {
			return;
		}
		boolean isAjax = request.isAjax();
		if(isAjax){
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_REAL_NAME;
			result.msg = "未实名认证";
			
			renderJSON(result);
		} else{
			flash.error("未实名认证");
			redirect("front.account.MySecurityCtrl.toRealNameIdentityPre");
		}
	}
	
	
	/**
	 * 企业认证
	 */
	@Before(priority=15)
	static void checkCompanyCer(){
		EnterpriseCertification companyCheck = getActionAnnotation(EnterpriseCertification.class);
		if (companyCheck == null) {
			return;
		}
		/* 获取登录当前对象 */
		CurrUser user = FrontBaseController.getCurrUser();
		if (user == null) {
			
			checkLogin();
		}
		if (user.is_company == 0) {
			return;
		}
		if (user.company_certif) {
			return;
		}
		
		boolean isAjax = request.isAjax();
		if(isAjax){
			ResultInfo result = new ResultInfo();
			result.code = ResultInfo.NOT_REAL_NAME;
			result.msg = "未企业认证";
			renderJSON(result);
		} else{
			flash.error("请先进行企业认证");
			redirect("front.account.MySecurityCtrl.toRealNameIdentityPre");
		}
	}
}
